﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using SIGERP_Datos.Properties;
using System.Data;

namespace SIGERP_Datos
{
    public class hndPaises
    {
        SqlConnection conexion = new SqlConnection(Conexion.CadenaConexion());
       
        public DataTable GetAll()
        {
            string select = "SELECT * FROM dbo.pais";
            SqlDataAdapter adapter = new SqlDataAdapter(select, conexion);
            DataTable tabla = new DataTable();
            try
            {
                adapter.Fill(tabla);
                conexion.Close();
            }
            catch (Exception exc)
            {
                throw new ArgumentException(exc.Message);
            }

            return tabla;
        }

        public DataRow GetOne(int id)
        {
            string select = "SELECT * FROM dbo.pais WHERE id_pais=" + id;
            SqlDataAdapter adapter = new SqlDataAdapter(select, conexion);
            DataTable tabla = new DataTable();
            try
            {
                adapter.Fill(tabla);
            }
            catch (Exception exc)
            {
                throw new ArgumentException(exc.Message);
            }

            return tabla.Rows[0];
        }

        public void Insert(SIGERP_Entidades.Pais pais)
        {
            SqlCommand comando = new SqlCommand();
            comando.Connection = conexion;
            comando.CommandText = "INSERT INTO Pais(descripcion) " +
                "VALUES (@descripcion)";
            // No necesito ingresar el ID.el id es autoincrementar y tiene una propriedad en la tabla en OFF
            //comando.Parameters.AddWithValue("@id_pais", pais.ID); 
            comando.Parameters.AddWithValue("@descripcion", pais.Nombre);
            try
            {
                conexion.Open();
                comando.ExecuteNonQuery();
                conexion.Close();
            }
            catch (Exception exc)
            {
                throw new ArgumentException(exc.Message);
            }
        }

        public void Update(SIGERP_Entidades.Pais pais)
        {
            SqlCommand comando = new SqlCommand();
            comando.Connection = conexion;
            comando.CommandText = "UPDATE dbo.pais SET descripcion=@descripcion " + "WHERE id_pais=" + pais.ID;
            comando.Parameters.AddWithValue("@descripcion", pais.Nombre);//pais.Nombre
            try
            {
                conexion.Open();
                comando.ExecuteNonQuery();
                conexion.Close();
            }
            catch (Exception exc)
            {
                throw new ArgumentException(exc.Message);
            }
        }

        public void Baja(int id)
        {
            SqlCommand comando = new SqlCommand();
            comando.Connection = conexion;
            comando.CommandText = "delete from pais where id_pais=" + id;
            try
            {
                conexion.Open();
                comando.ExecuteNonQuery();
                conexion.Close();
            }
            catch (Exception exc)
            {
                throw new ArgumentException(exc.Message);
            }
        }
    }
}
